﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;

public partial class Hoso_QuanlyPhumuc : System.Web.UI.Page
{
    private void DisplayMessage(string text)
    {
        RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
    }

    private void SetMessage(string message)
    {
        gridMessage = message;
    }

    private string gridMessage = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tenthanhvien"] == null) Response.Redirect("../LogIn.aspx");
    }
    protected void RadGrid1_DataBound(object sender, EventArgs e)
    {

        if (!string.IsNullOrEmpty(gridMessage))
        {
            DisplayMessage(gridMessage);
        }
    }
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked
        {
            GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColum");
            editColumn.Visible = false;
        }
        else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)
        {
            e.Canceled = true;
        }
        else
        {
            GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColum");

            if (!editColumn.Visible)
                editColumn.Visible = true;
        }
    }
    protected void RadGrid1_ItemDeleted(object sender, Telerik.Web.UI.GridDeletedEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;

        String id = dataItem.GetDataKeyValue("maphumuc").ToString();

        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            SetMessage("Phumuc with ID " + id + " cannot be deleted. Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("Phumuc with ID " + id + " is deleted!");
        }
    }
    protected void RadGrid1_ItemInserted(object sender, Telerik.Web.UI.GridInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            e.KeepInInsertMode = true;
            DisplayMessage(true, "phumuc cannot be inserted. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(false, "phumuc inserted");
        }
    }
    protected void RadGrid1_ItemUpdated(object sender, Telerik.Web.UI.GridUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.KeepInEditMode = true;
            e.ExceptionHandled = true;
            DisplayMessage(true, "Phu muc " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["maphumuc"] + " cannot be updated. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(false, "Phu muc " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["maphumuc"] + " updated");
        }
    }
    private void DisplayMessage(bool isError, string text)
    {
        Label label = (isError) ? this.Label1 : this.Label2;
        label.Text = text;
    }
}
